﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace mhxp
{
	public partial class ReadFileForm : Form
	{
		private HashSet<string> _accexts = new HashSet<string>();

		private Thread _thd;
		private bool _work;

		private List<ListViewItem> _items = new List<ListViewItem>();
		private int _total;

		private string[] _paths;
		private string _lastpath;
		private Point _pixsize;

		private delegate void DgtDoClose();
		private delegate void DgtUpdateRead();

		public ReadFileForm(string[] paths, string lastpath, Point pixsize)
		{
			InitializeComponent();

			//
			_accexts.Add("png");
			_accexts.Add("bmp");
			//_accexts.Add("tga");
			_accexts.Add("jpg");
			_accexts.Add("jpeg");

			//
			_paths = paths;
			_lastpath = lastpath;
			_pixsize = pixsize;
		}

		private void ReadFileForm_Load(object sender, EventArgs e)
		{
			_work = true;
			_thd = new Thread(new ThreadStart(ThreadProc));
			_thd.Start();
		}

		private void ReadFileForm_FormClosing(object sender, FormClosingEventArgs e)
		{

		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			_work = false;
			_thd.Join();

			_items.Clear();

			this.Close();
		}

		public List<ListViewItem> Items
		{
			get { return _items; }
		}

		public string LastPath
		{
			get { return _lastpath; }
		}

		public Point PixSize
		{
			get { return _pixsize; }
		}

		private void UpdateRead()
		{
			if (this.InvokeRequired)
			{
				if (_work)
					this.Invoke(new DgtUpdateRead(UpdateRead));
			}
			else
			{
				int cnt = _items.Count > _total ? _total : _items.Count;

				prgRead.Maximum = _total;
				prgRead.Value = cnt;

				lblRead.Text = string.Format("{0} / {1}", cnt, _total);
			}
		}

		private void ThreadProc()
		{
			_total = _paths.Length;

			foreach (string s in _paths)
			{
				if (!_work)
					break;

				if (!AddPixFile(s))
					_total--;

				UpdateRead();
			}

			if (_work)
				this.Invoke(new DgtDoClose(Close));
		}

		private void BuildLastPath(string path, bool isfilename)
		{
			if (_lastpath.Length != 0)
				return;

			if (!isfilename)
				_lastpath = path;
			else
			{
				FileInfo fi = new FileInfo(path);
				_lastpath = fi.DirectoryName;
			}
		}

		private bool AddPixFile(string filename)
		{
			if (!_work)
				return false;

			if (Directory.Exists(filename))
			{
				// 디렉터리임
				DirectoryInfo di = new DirectoryInfo(filename);
				BuildLastPath(di.FullName, false);

				FileInfo[] fis = di.GetFiles();

				_total += fis.Length;

				foreach (FileInfo fi in fis)
				{
					if (!AddPixFile(fi.FullName))
						_total--;

					UpdateRead();
				}
			}
			else if (File.Exists(filename))
			{
				// 파일임
				FileInfo fi = new FileInfo(filename);
				BuildLastPath(filename, true);

				if (fi.Extension.Length < 3)
					return false;

				string ext = fi.Extension.Substring(1).ToLower();
				Bitmap bmp;

				if (_accexts.Contains(ext))
					bmp = new Bitmap(filename);
				else if (ext == "tga")
					bmp = Paloma.TargaImage.LoadTargaImage(filename);
				else
				{
					// 처리 불가
					return false;
				}

				bmp.Tag = fi.Name;

				if (_pixsize.X == 0 && _pixsize.Y == 0)
				{
					_pixsize.X = bmp.Width;
					_pixsize.Y = bmp.Height;
				}
				else
				{
					if (_pixsize.X != bmp.Width || _pixsize.Y != bmp.Height)
					{
						// 햐~ 크기가 다른 놈은 안뒤야~!!
						return false;
					}
				}

				ListViewItem li = new ListViewItem(fi.Name);
				li.SubItems.Add(fi.Length.ToString());
				li.Tag = bmp;

				_items.Add(li);
			}

			return true;
		}
	}
}
